/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package administrador;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Comparator;
import javax.swing.*;
import javax.swing.event.RowSorterEvent;
import javax.swing.event.RowSorterListener;
import javax.swing.table.*;
 
public class TablePrepareRenderer extends JFrame
{
	JTable table;
        DefaultTableModel model;
        JScrollPane scrollPane;
        Object[][] data;
        Object[] columnNames;
        
        JButton button;
        JButton button2;
        JButton button3;
        JButton button4;
        JPanel buttonPanel;
 
        public static void main(String[] args)
	{
		TablePrepareRenderer frame = new TablePrepareRenderer();
                frame.setTitle("Cell Renderer");
                frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
		frame.pack();
		frame.setLocationRelativeTo( null );
		frame.setVisible(true);
	}
        
	public TablePrepareRenderer()
	{
                setPreferredSize(new Dimension(600, 300));
            
		final Object[] columnNames = {"UID", "State", "Result", "Time Elapsed"};
                
		final Object[][] data =
		{
			{"1", "Ready", "N/A", new Float(80.50)},
			{"2", "Forced Running", "N/A", new Float(6.25)},
			{"3", "Paused", "N/A", new Float(7.35)},
			{"4", "Ready", "Fail", new Float(20.00)},
                        {"5", "Ready", "Pass (With Warnings)", new Float(32.00)}
		};
 
		model = new DefaultTableModel(data, columnNames);
                
                JTable table2 = new JTable(model){
                    
                    public Class getColumnClass(int column)
                    {
                        return getValueAt(0, column).getClass();
                    }
                    
                    public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
                    {
                        Component c = super.prepareRenderer(renderer, row, column);
                        
                        return c;
                    }
                };
                
		table = new JTable( model )
		{
			//  Returning the Class of each column will allow different
			//  renderers to be used based on Class
			public Class getColumnClass(int column)
			{
				return getValueAt(0, column).getClass();
			}
 
			public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
			{
				Component c = super.prepareRenderer(renderer, row, column);
 
                                        TableColumnModel tcm = table.getColumnModel();
 
                                        String stateType = (String)getValueAt(row, tcm.getColumnIndex("State"));
                                
                                        if(stateType.equalsIgnoreCase("Forced Running"))
                                        {
                                            c.setBackground(new Color(0, 150, 255));
                                            c.setForeground(new Color(0,0,0));
                                            setSelectionBackground(new Color(50, 200, 255));
                                        }
                                        else if(stateType.equalsIgnoreCase("Paused"))
                                        {
                                            c.setBackground(new Color(255, 220, 45));
                                            c.setForeground(new Color(0,0,0));
                                            setSelectionBackground(new Color(255, 240, 100));
                                        }
                                        
                                        String resultType = (String)getValueAt(row, tcm.getColumnIndex("Result"));
                                        
                                        if(resultType.equalsIgnoreCase("Fail"))
                                        {
                                            c.setBackground(new Color(255, 50, 50));
                                            c.setForeground(new Color(0,0,0));
                                            setSelectionBackground(new Color(255, 100, 100));
                                        }
                                        else if(resultType.equalsIgnoreCase("Pass (With Warnings)"))
                                        {
                                            c.setBackground(new Color(50, 255, 50));
                                            c.setForeground(new Color(0,0,0));
                                            setSelectionBackground(new Color(150, 255, 200));
                                        }
                                        else if(resultType.equalsIgnoreCase("Pass"))
                                        {
                                            c.setBackground(new Color(50, 255, 50));
                                            c.setForeground(new Color(0,0,0));
                                            setSelectionBackground(new Color(150, 255, 200));
                                        }
                                        
                                        if(stateType.equalsIgnoreCase("Ready") && resultType.equalsIgnoreCase("N/A"))
                                        {
                                            c.setBackground(new Color(255,255,255));
                                            c.setForeground(new Color(0,0,0));
                                        }                                   
 
				return c;
			}
		};
                
                
                TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
                table.setRowSorter(sorter);                                         // Gives the Table a Row Sorter
                
                sorter.addRowSorterListener(new RowSorterListener(){                // This is a action listener for when the user presses on sort
                
                        public void sorterChanged(RowSorterEvent e){                // Produces the event when something is heard
                            sortAction(e);                                          // This is the method that is called when an action occurs
                        }
                });
                
                Comparator<String> comparator = new Comparator<String>(){           // Comparing Algorithm
                    @Override
                    public int compare(String s1, String s2){
                        String first = s1;
                        String second = s2;
                        
                        return first.compareTo(second);
                    }
                };
                
                sorter.setComparator(1, comparator);                                // Utilizes this particular comparator
                
		table.setPreferredScrollableViewportSize(table.getPreferredSize());
                
                button = new JButton("Move Up");
                button.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent aem){
                        moveUpAction(aem);
                    }
                });
                
                button2 = new JButton("Move Down");
                button2.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent aem2){
                        moveDownAction(aem2);
                    }
                });
                
                button3 = new JButton("Move Top");
                button3.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent aem3){
                        moveTopAction(aem3);
                    }
                });
                
                button4 = new JButton("Move Bottom");
                button4.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent aem4){
                        moveBottomAction(aem4);
                    }
                });
                
                buttonPanel = new JPanel();
                buttonPanel.add(button);
                buttonPanel.add(button2);
                buttonPanel.add(button3);
                buttonPanel.add(button4);
                
		scrollPane = new JScrollPane( table );
		getContentPane().add( scrollPane, BorderLayout.NORTH );
                getContentPane().add(buttonPanel, BorderLayout.SOUTH);
	}
        
        public void sortAction(RowSorterEvent e)
        {
            System.out.println("Testing Testing");
        }
        
        public void moveUpAction(ActionEvent aem){
            if(table.getSelectedRow() == 0)
            {
                // You cannot move a row that is at the top
            }
            else
            {
                  int rowToMove = table.convertRowIndexToModel(table.getSelectedRow());
                  int destination = table.convertRowIndexToModel(table.getSelectedRow() - 1);
 
                  model.moveRow(rowToMove, rowToMove, destination);
            }
        }
        
        public void moveDownAction(ActionEvent aem2)
        {
            if(table.getSelectedRow() == (table.getRowCount()-1))
            {
                // Cannot move down a row that is at the bottom of the table
            }
            else
            {
 
            }
        }
        
        public void moveTopAction(ActionEvent aem3)
        {
            if(table.getSelectedRow() == 0)
            {
                // Dont move to top if it is the top row already
            }
            else
            {
 
            }
        }
        
        public void moveBottomAction(ActionEvent aem4)
        {
            if(table.getSelectedRow() == (table.getRowCount()-1))
            {
                // Dont move to the bottom if it is the bottom row already
            }
            else
            {
 
            }
        }
        
}
